package com.zhb.lkd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhb.lkd.base.PageResponse;
import com.zhb.lkd.exception.BusinessException;
import com.zhb.lkd.mapper.RegionMapper;
import com.zhb.lkd.mapper.VmTypeMapper;
import com.zhb.lkd.pojos.entity.Node;
import com.zhb.lkd.pojos.entity.Region;
import com.zhb.lkd.pojos.entity.VendingMachine;
import com.zhb.lkd.mapper.VendingMachineMapper;
import com.zhb.lkd.pojos.entity.VmType;
import com.zhb.lkd.pojos.vo.NodeVo;
import com.zhb.lkd.pojos.vo.RegionVo;
import com.zhb.lkd.pojos.vo.VmListNodeVo;
import com.zhb.lkd.service.INodeService;
import com.zhb.lkd.service.IVendingMachineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author haibin
 * @since 2024-08-18
 */
@Service
public class VendingMachineServiceImpl extends ServiceImpl<VendingMachineMapper, VendingMachine> implements IVendingMachineService {
    @Resource
    private NodeServiceImpl nodeService;
    @Resource
    private RegionMapper regionMapper;
    @Resource
    private VmTypeMapper vmTypeMapper;
    /**
     * 设备搜索
     *
     * @param pageIndex
     * @param pageSize
     * @param innerCode
     * @return
     */
    @Override
    public PageResponse search(Integer pageIndex, Integer pageSize, String innerCode) {
        //1.检查参数
        if (pageIndex == null || pageSize == null) {
            throw new BusinessException(500, "参数错误");
        }
        //2.分页查询
        Page<VendingMachine> page = lambdaQuery().like(StrUtil.isNotBlank(innerCode), VendingMachine::getInnerCode, innerCode)
                .page(new Page<>(pageIndex, pageSize));
        //封装VmListNodeVo 对象
        List<VmListNodeVo> vmListNodeVos=new ArrayList<>();
        //将VendingMachine存储到vmListNodeVos
        List<VendingMachine> vendingMachines = page.getRecords();
        //封装VmListNodeVo
        for (VendingMachine vendingMachine : vendingMachines) {
            //克隆参数
            VmListNodeVo vmListNodeVo = BeanUtil.copyProperties(vendingMachine, VmListNodeVo.class);
            //根据vmTypeid查询
            VmType vmType = vmTypeMapper.selectById(vendingMachine.getVmType());
            vmListNodeVo.setType(vmType);
            //根据nodeid查询node表
            Node node = nodeService.getById(vendingMachine.getNodeId());
            NodeVo nodeVo = nodeService.nodeToNodeVo(node);
            vmListNodeVo.setNode(nodeVo);
            //根据regionId查询region表
            Region region = regionMapper.selectById(vendingMachine.getRegionId());
            RegionVo regionVo = BeanUtil.copyProperties(region, RegionVo.class);
            regionVo.setName(region.getRegionName());
            //todo nodeList没写
            vmListNodeVo.setRegion(regionVo);
            //添加到集合
            vmListNodeVos.add(vmListNodeVo);
        }
        return new PageResponse(page,vmListNodeVos);
    }
}
